package org.hcf.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.hcf.data.entity.ChatGroup;
import org.hcf.data.mapper.ChatGroupMapper;
import org.hcf.data.service.IChatGroupService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 群组 服务实现类
 * </p>
 *
 * @author xianiyitiao
 * @since 2024-04-18
 */
@Service
public class ChatGroupServiceImpl extends ServiceImpl<ChatGroupMapper, ChatGroup> implements IChatGroupService {

    @Override
    public List<ChatGroup> getGroups(List<Long> groupIds) {
        return list(new LambdaQueryWrapper<ChatGroup>().eq(ChatGroup::isDispose, false).in(ChatGroup::getGroupId, groupIds));
    }

    @Override
    public boolean updateReceive(long id, boolean isReceive) {
        return update(new LambdaUpdateWrapper<ChatGroup>().eq(ChatGroup::getGroupId, id).set(ChatGroup::isReceive, isReceive));
    }

    @Override
    public List<Long> getFriendsByLastTime(List<Long> groupIds, LocalDateTime lastTime) {
        return list(new LambdaQueryWrapper<ChatGroup>().select(ChatGroup::getGroupId).in(ChatGroup::getGroupId).gt(ChatGroup::getModifyTime, lastTime)).stream().map(ChatGroup::getGroupId).collect(Collectors.toList());
    }

    @Override
    public Long getByAccount(String groupAccount) {
        ChatGroup group = getOne(new LambdaQueryWrapper<ChatGroup>().eq(ChatGroup::getGroupAccount, groupAccount).last("limit 1"));
        if (group != null) return group.getGroupId();
        return null;
    }
}
